package cn.edu.cqu.fredyvia.forum.service.impl;

import cn.edu.cqu.fredyvia.forum.Utils.Utils;
import cn.edu.cqu.fredyvia.forum.config.AliyunOssConfig;
import cn.edu.cqu.fredyvia.forum.dto.TalkUserinfoDTO;
import cn.edu.cqu.fredyvia.forum.entity.Talk;
import cn.edu.cqu.fredyvia.forum.mapper.TalkMapper;
import cn.edu.cqu.fredyvia.forum.service.ITalkService;
import cn.edu.cqu.fredyvia.forum.storage.service.AliyunOssStorageProvider;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fredyvia
 * @since 2021-07-10
 */
@Service
@Slf4j
public class TalkServiceImpl extends ServiceImpl<TalkMapper, Talk> implements ITalkService {
//    @Autowired
//    UserInfoMapper userInfoMapper;
//    @Override
//    public List<Talk> list() {
//
//        List<Talk> talkList = super.list();
//        for (Talk talk : talkList) {
//            talk.setUserInfo(userInfoMapper.selectById(talk.getUserId()));
//        }
//        return talkList;
//    }
    @Autowired
    TalkMapper talkMapper;

    @Autowired
    AliyunOssConfig aliyunOssConfig;

    @Autowired
    AliyunOssStorageProvider aliyunOssStorageProvider;
    //默认排序
//    @Override
//    public List<TalkUserinfoDTO> select_inject(Integer from, Integer to) {
//        String type1=TOTAL;
//        String type2=CREATE_DATETIME;
//        return talkMapper.select_inject(from, to,type1,type2);
//    }

    //类型排序
    @Override
    public List<TalkUserinfoDTO> select_inject(Integer from, Integer to,String type) {
        return talkMapper.select_inject(from, to,type);
    }

    @Override
    public String save2oss(String content){
        String fileName= Utils.getRandomFileName("md");
        String path=aliyunOssConfig.getContentBasepath()+"/"+fileName;
        log.info("save2oss:"+path);
        ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes());
        try {
            aliyunOssStorageProvider.save(is, Paths.get(path));
        } catch (IOException e) {
            log.info(e.toString());
        }

        return path;
    }

    public TalkUserinfoDTO getUserinfoDto(Integer talkId){
        return talkMapper.getUserinfoDto(talkId);
    }

}
